/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.EnumSet;

import org.lwjgl.util.Rectangle;

import fi.honkalampisaatio.lmodeller.LanguageResource;
import fi.honkalampisaatio.lmodeller.LModeller;
import fi.honkalampisaatio.lmodeller.Logger;
import fi.honkalampisaatio.lmodeller.selection.GameModeSelectionEvent;
import fi.honkalampisaatio.lmodeller.selection.UserSelectionEvent;
import fi.honkalampisaatio.lmodeller.selection.GameModeSelectionEvent.MODES;
import fi.honkalampisaatio.lmodeller.ui.GameButton.TEXTLOCATION;
import fi.honkalampisaatio.lmodeller.xml.User;
import glapp.GLApp;

public class UserMenu extends GameView implements ActionListener{

	GameTextLabel instructionLabel;
	GameButton exitButton;
	boolean showExit;
	
	public UserMenu(boolean showExit)
	{
		super(EnumSet.of(BUTTONFLAGS.VOICE, BUTTONFLAGS.UPLABEL));
		this.showExit = showExit;
	}
	
	@Override
	public void init() {
		setUpLabelText(LanguageResource.getInstance().get("CHOOSE_USER_TITLE"));
		setUpToolTipSentence(LanguageResource.getInstance().get("CHOOSE_USER_TITLE"));
    	if(LModeller.getGameSetting() != null && LModeller.getGameSetting().getUsers() != null)
    	{
        	ArrayList<User> userList = LModeller.getGameSetting().getUsers();
        	ArrayList<User> activeUsers = new ArrayList<User>();
        	for(int i=0; i<userList.size();i++)
        	{
    			User user = userList.get(i);
    			if(user.isActive())
    			{
    				activeUsers.add(user);
    			}
        	}
        	int rows = (int)Math.sqrt(activeUsers.size());
    		int columns = rows;
    		if(activeUsers.size()>columns*rows)
    			columns++;
    		if(activeUsers.size()>columns*rows)
    			rows++;
        	int startY = (int)(GLApp.getHeight()*0.9);
        	int startX = (int)(GLApp.getWidth()*0.1);
        	int h = (int)(GLApp.getHeight()*0.8/rows);
        	int w = (int)(GLApp.getWidth()*0.8/columns);
        	for(int y=0; y<rows; y++)
        	{
        		for(int x=0; x<columns && (x+y*columns)<activeUsers.size(); x++)
        		{
        			User user = activeUsers.get(x+y*columns);
                	UserButton uButton = new UserButton(new Rectangle((int)(w*x + w*0.1 + startX), 
                			(int)(startY-h*(y+1)+ h*0.1f), (int)(w*0.8f), (int)(h*0.8f)), 
                			user);
                	uButton.setToolTipSentence(user.getName());
                	uButton.setActionCommand("USER");
                	uButton.addActionListener(this);
                	super.addComponent(uButton);
        		}
        	}
    	}
    	if(showExit)
    	{
        	int h = GLApp.getHeight()/10;
        	int w = GLApp.getWidth()/10;
    		exitButton = new GameButton(new Rectangle((int)(w*9.1),(int)(h*9.1),
        			(int)(w*0.8),(int)(h*0.8)), GameTextures.getInstance().getTexture("EXIT_BUTTON"), LModeller.actionColor
        			, null, TEXTLOCATION.UP);
    		exitButton.setActionCommand("EXIT");
    		exitButton.setToolTipSentence(LanguageResource.getInstance().get("EXIT_BUTTON"));
    		exitButton.addActionListener(this);
        	super.addComponent(exitButton);
    	}
	}

	public void actionPerformed(ActionEvent e) { 
		System.out.println("UserMenu actionPerformed");
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("USER") && e.getSource() instanceof UserButton)
		{
			Logger.getInstance().log("User selected");
			UserButton button = (UserButton)e.getSource();
			if(button.getUser()!=null)
				System.out.println("User name " + button.getUser().getName());
			else
				System.out.println("User is null");
			UserSelectionEvent se = new UserSelectionEvent(this, button.getUser(), button.getTexture());
			this.fireSelectionEvent(se);
		}
		if(e.getSource() !=null && e.getActionCommand().equalsIgnoreCase("EXIT") && e.getSource() instanceof GameButton)
		{
			Logger.getInstance().log("Exit button selected");
			GameModeSelectionEvent se = new GameModeSelectionEvent(this, MODES.EXIT);
			fireSelectionEvent(se);
		}
	}

	@Override
	protected void refreshPage(int page) {
	}


}
